home *** CD-ROM | disk | FTP | other *** search
- Path: proffa.cc.tut.fi!m151843
- From: m151843@proffa.cc.tut.fi (Edvard Majakari)
- Newsgroups: comp.lang.c++
- Subject: Yet a question about random
- Date: 25 Feb 1996 12:47:24 GMT
- Organization: Heart of Gold, Milk-O-Matic & Bicep-O-Bulgizer
- Distribution: world
- Message-ID: <4gplos$lds@cc.tut.fi>
- NNTP-Posting-Host: proffa.cc.tut.fi
- X-Newsreader: TIN [version 1.2 PL2]
-
- I use the following function to produce (pseudo)random numbers
- between 1...top. However, this algorithm seems to be far from
- good pseudorandom. But the hint was in rand manpage - any good
- ideas to produce better 'random' numbers?
-
- -----&<------------------------&<-------------------------------
-
- #include <stdlib.h>
- #include <sys/time.h>
- #include <unistd.h>
-
- int randomize(float top) {
- int rnum;
- timeval seed; //see struct timeval for further details
- gettimeofday(&seed, NULL);
- srand(seed.tv_usec); //initialize with millisecs
- rnum = 1+(int) (top*rand()/(RAND_MAX+1.0));
- return rnum;
- }
-
- -----&<------------------------&<-------------------------------
- --
- //Ed http://lodge.ton.tut.fi/~ed/
- %
- Fact : I ache , therefore I am...or in my case I am therefore I ache.
- It reminds me of that thing, what was it Oh yes...life......maybe
- if I just lie down and ignore it, it will go away....or perhaps
- not..... Zootlewerdle zootlewerdle zootlewerdle (Marvin Lying in a
- deep dark hole). -- from HHGTTG
-